<template>
  <el-dialog
    :title="!dataForm.spellgroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="头像" prop="spellgroupImages">
      <el-input v-model="dataForm.spellgroupImages" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="团长" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="团长"></el-input>
    </el-form-item>
    <el-form-item label="开团时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="开团时间"></el-input>
    </el-form-item>
    <el-form-item label="拼团商品" prop="spellgroupShop">
      <el-input v-model="dataForm.spellgroupShop" placeholder="拼团商品"></el-input>
    </el-form-item>
    <el-form-item label="几人团" prop="spellgroupCount">
      <el-input v-model="dataForm.spellgroupCount" placeholder="几人团"></el-input>
    </el-form-item>
    <el-form-item label="几人参与" prop="spellgroupCountPeople">
      <el-input v-model="dataForm.spellgroupCountPeople" placeholder="几人参与"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="拼团状态" prop="spellgroupStatus">
      <el-input v-model="dataForm.spellgroupStatus" placeholder="拼团状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          spellgroupId: 0,
          spellgroupImages: '',
          userId: '',
          createTime: '',
          spellgroupShop: '',
          spellgroupCount: '',
          spellgroupCountPeople: '',
          endTime: '',
          spellgroupStatus: ''
        },
        dataRule: {
          spellgroupImages: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '团长不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '开团时间不能为空', trigger: 'blur' }
          ],
          spellgroupShop: [
            { required: true, message: '拼团商品不能为空', trigger: 'blur' }
          ],
          spellgroupCount: [
            { required: true, message: '几人团不能为空', trigger: 'blur' }
          ],
          spellgroupCountPeople: [
            { required: true, message: '几人参与不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          spellgroupStatus: [
            { required: true, message: '拼团状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.spellgroupId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.spellgroupId) {
            this.$http({
              url: this.$http.adornUrl(`/system/spellgroup/info/${this.dataForm.spellgroupId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.spellgroupImages = data.spellGroup.spellgroupImages
                this.dataForm.userId = data.spellGroup.userId
                this.dataForm.createTime = data.spellGroup.createTime
                this.dataForm.spellgroupShop = data.spellGroup.spellgroupShop
                this.dataForm.spellgroupCount = data.spellGroup.spellgroupCount
                this.dataForm.spellgroupCountPeople = data.spellGroup.spellgroupCountPeople
                this.dataForm.endTime = data.spellGroup.endTime
                this.dataForm.spellgroupStatus = data.spellGroup.spellgroupStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/spellgroup/${!this.dataForm.spellgroupId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'spellgroupId': this.dataForm.spellgroupId || undefined,
                'spellgroupImages': this.dataForm.spellgroupImages,
                'userId': this.dataForm.userId,
                'createTime': this.dataForm.createTime,
                'spellgroupShop': this.dataForm.spellgroupShop,
                'spellgroupCount': this.dataForm.spellgroupCount,
                'spellgroupCountPeople': this.dataForm.spellgroupCountPeople,
                'endTime': this.dataForm.endTime,
                'spellgroupStatus': this.dataForm.spellgroupStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
